﻿@using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate
@using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate
<a @onclick="ShowModal">配置角色</a>
<Modal Title="@("配置角色")"
       Visible="@_modalVisible"
       OnOk="Modal_HandleOk"
       OnCancel="() =>  _modalVisible = false"
       ConfirmLoading="@_modalConfirmLoading">

    <CheckboxGroup Options="@_roleOptions" @bind-Value="@_selectedRoleIds" TValue="RoleId"/>
</Modal>

@code {
    [CascadingParameter] public AdminUserResponse Row { get; set; } = default!;

    [Inject] private IAdminUserService AdminUserService { get; set; } = default!;
    [Inject] private MessageService Message { get; set; } = default!;

    private CheckboxOption<RoleId>[] _roleOptions = [];
    private RoleId[] _selectedRoleIds = [];
    
    public record RolesUpdateSucceededEventArgs(AdminUserId AdminUserId, IEnumerable<string> RoleNames);
    [Parameter] public EventCallback<RolesUpdateSucceededEventArgs> OnUpdateSucceeded { get; set; }

    private bool _modalVisible;
    private bool _modalConfirmLoading;

    private async Task ShowModal()
    {
        _modalVisible = true;
        var roles = await GetAdminUserRoles(Row.Id);
        _roleOptions = roles.Select(x => new CheckboxOption<RoleId>
        {
            Label = x.RoleName,
            Value = x.RoleId,
            Checked = x.IsAssigned
        }).ToArray();
    }

    private async Task<List<AdminUserRoleResponse>> GetAdminUserRoles(AdminUserId id)
    {
        var response = await AdminUserService.GetAdminUserRoles(id);
        if (response.Success) return response.Data.ToList();
        _ = Message.Error(response.Message);
        return [];
    }

    private async Task Modal_HandleOk()
    {
        _modalConfirmLoading = true;
        StateHasChanged();
        var response = await AdminUserService.UpdateAdminUserRoles(Row.Id, _selectedRoleIds);
        _modalConfirmLoading = false;
        if (response.Success)
        {
            _modalVisible = false;
            _ = Message.Success("更新成功！");
            await OnUpdateSucceeded.InvokeAsync(new RolesUpdateSucceededEventArgs(
                Row.Id,
                _roleOptions.Where(r => _selectedRoleIds.Contains(r.Value)).Select(r => r.Label)));
        }
        else
        {
            _ = Message.Error(response.Message);
        }
    }

}